package guda.mrpc.core.client.loadblance.impl;


import guda.mrpc.core.RequestKey;
import guda.mrpc.core.client.ConnectionFactory;
import guda.mrpc.core.client.RpcInvoker;
import guda.mrpc.core.client.loadblance.LoadBalance;
import guda.mrpc.core.common.util.AtomicRangeInteger;
import guda.mrpc.core.transport.ClientHandler;

import java.util.List;

/**
 * Created by well on 17/5/21.
 */
public class RoundLoadBalance implements LoadBalance {

    AtomicRangeInteger atomicRangeInteger=new AtomicRangeInteger(0);


    @Override
    public ClientHandler select(RequestKey requestKey) throws InterruptedException {
        List<ClientHandler> handlers = ConnectionFactory.instance().getHandlers(requestKey);
        if(handlers == null || handlers.size()==0){
            return null;
        }
        return handlers.get(atomicRangeInteger.getAndIncrement()%handlers.size());
    }
}
